/**
 * 
 */
package org.gov.vis.data.controller;

import java.util.Date;
import java.util.List;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.common.exceptions.SystemException;
import org.gov.vis.data.dao.DistributionLnDao;
import org.gov.vis.data.dao.DistributionLnDaoImpl;
import org.gov.vis.data.dto.DistributionLnDto;
import org.gov.vis.util.DBConstants;
import org.gov.vis.util.DU;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.ValidatorUtils;

/**
 * @author user
 *
 */
public class DistributionLnCtlrImpl implements DistributionLnCtlr {

	/**
	 * Method called to Add or Edit the ProcurementLN
	 * @param prLnDto
	 * @throws Exception
	 */

	public void addOrEditDisLN(final DistributionLnDto _disLnDto) throws Exception {
		try{
			/*
			 * Below set of validations check for Required/Mandatory fields
			 */
			String _distributionLnCodeErr = ValidatorUtils.validateString(rb.getString("dialog_label_dist_ln_code"), _disLnDto.getCode(), 1, 20, true);
			if(_distributionLnCodeErr != null)
				errorMsgs.put(rb.getString("dialog_label_dist_ln_code").toUpperCase(), _distributionLnCodeErr);

			
			String _distributionLnFromStationErr= ValidatorUtils.validateString(rb.getString("dialog_label_dist_ln_frmstation"), _disLnDto.getFrmStation(), 1, 20, true);
			if(_distributionLnFromStationErr != null)
				errorMsgs.put(rb.getString("dialog_label_dist_ln_frmstation").toUpperCase(), _distributionLnFromStationErr);

			String _distributionLnNameOrgErr = ValidatorUtils.validateString(rb.getString("dialog_label_dist_ln_vehiceno"), _disLnDto.getVehicleNo(), 4, 50, true);
			if(_distributionLnNameOrgErr != null)
				errorMsgs.put(rb.getString("dialog_label_dist_ln_vehiceno").toUpperCase(), _distributionLnNameOrgErr);
			
			/*
			 * Below are NOT required fields but are validated on their data type 
			 */
			String _disLNKgsErr = ValidatorUtils.validateDecimal(rb.getString("dialog_label_dist_ln_kgs"), _disLnDto.getKgs(), ValidatorUtils.DECIMALS, 0, Double.MAX_VALUE, 0, 2, false, false);
			if(_disLNKgsErr != null)
				errorMsgs.put(rb.getString("dialog_label_dist_ln_kgs").toUpperCase(), _disLNKgsErr);
			String _disSemenDODistrErr = ValidatorUtils.validateString(rb.getString("dialog_label_dist_ln_dtofdstr"), DU.format(_disLnDto.getDoDstrbut(), DBConstants.DISPLAY_DATE_FORMAT), 6, 50, true);
			if(_disSemenDODistrErr != null)
				errorMsgs.put(rb.getString("dialog_label_dist_ln_dtofdstr").toUpperCase(), _disSemenDODistrErr);


			if (errorMsgs != null && !errorMsgs.isEmpty())
				throw new InvalidInputException();

			/*
			 *  Below set of validations check for duplicate records
			 * 
			 */

		//check if there exists andistributionLn DistributionLn Object with the same Code
		//If Code has been duplicated, then throw the alert
		DistributionLnDto distributionLnDto = null;
		if(_disLnDto.getDisLnId() > 0){
			//get the DistributionLnDto on the passed Code name
			distributionLnDto = this.getDisLnOnCode(_disLnDto.getCode());
			if(distributionLnDto != null && distributionLnDto.getDisLnId() != _disLnDto.getDisLnId()){
				errorMsgs.put(rb.getString("dialog_label_dist_ln_code").toUpperCase(), rb.getString(GuiConstants.DATA_DUPLICATE_DISTRIBUTIONLN_MSG));
			}
		}//end if
		else{
			//get the DistributionLnDto on the passed Code name
			distributionLnDto = this.getDisLnOnCode(_disLnDto.getCode());
			if(distributionLnDto != null){
				errorMsgs.put(rb.getString("dialog_label_dist_ln_code").toUpperCase(), rb.getString(GuiConstants.DATA_DUPLICATE_DISTRIBUTIONLN_MSG));
			}
		}//end else
				
			if (errorMsgs != null && !errorMsgs.isEmpty())
				throw new InvalidInputException();

			final DistributionLnDao _prLnDao = new DistributionLnDaoImpl();

			//add or edit the Distribution LN Object
			_prLnDao.addOrEditDistributionLn(_disLnDto);
		}
		catch(InvalidInputException ie){
			throw ie;
		}
		catch (Exception ex) {
			ex.printStackTrace();
			if(_disLnDto.getDisLnId() > 0){
				throw new SystemException(rb.getString(GuiConstants.DATA_EDIT_DISTRBUTIONLN_EXCEPTION));
			}
			else{
				throw new SystemException(rb.getString(GuiConstants.DATA_ADD_DISTRBUTIONLN_EXCEPTION));
			}
		}
	}//end addOrEditDisLN

	/**
	 * Method called to delete the ProcurementLN Object based on the passed prSemen Id
	 * @param _disLnId
	 * @throws Exception
	 */

	public void deleteDisLN(final long _disLnId) throws Exception {
		try{
			final DistributionLnDao _disLnDao = new DistributionLnDaoImpl();
			_disLnDao.deleteDistributionLn(_disLnId);
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.DATA_DELETE_DISTRBUTIONLN_EXCEPTION));
		}
	}//end deleteDisLN

	/**
	 * Method called to get the ProcurementLN based on the passed primary key Id
	 * @param _disLnId
	 * @return
	 * @throws Exception
	 */

	public DistributionLnDto getDisLN(final long _disLnId) throws Exception {
		try{
			final DistributionLnDao _disLnDao = new DistributionLnDaoImpl();
			DistributionLnDto _disLnDto = new DistributionLnDto();
			_disLnDto.setDisLnId(_disLnId);

			DistributionLnDto prLnDto = null;
			List<DistributionLnDto> _disSemenList = _disLnDao.getDistributionLns(_disLnDto);
			if(_disSemenList != null && !_disSemenList.isEmpty()){
				prLnDto = _disSemenList.get(0);
			}

			return prLnDto;
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.DATA_VIEW_EDIT_DISTRBUTIONLN_EXCEPTION));
		}
	}//end getDisLN


	/**
	 * Method called to get the ProcurementLN based on the passed DstributedDate
	 * @param _disLnDistDt
	 * @return
	 * @throws Exception
	 */

	public DistributionLnDto getDisLNOnDtOnDstr(final Date _disLnDistDt) throws Exception {
		final DistributionLnDao _disLnDao = new DistributionLnDaoImpl();
		DistributionLnDto _disLnDto = new DistributionLnDto();
		_disLnDto.setDoDstrbut(_disLnDistDt);

		DistributionLnDto disLnDto = null;
		List<DistributionLnDto> _disSemenList = _disLnDao.getDistributionLns(_disLnDto);
		if(_disSemenList != null && !_disSemenList.isEmpty()){
			disLnDto = _disSemenList.get(0);
		}

		return disLnDto;
	}//end getDisLNOnDtOnDstr

	/**
	 * Method called to get the ProcurementLN based on the passed VehicleNo
	 * @param _disLnVhclNo
	 * @return
	 * @throws Exception
	 */

	public DistributionLnDto getDisLNOnVehcleNo(final String _disLnVhclNo) throws Exception {
		final DistributionLnDao _disLnDao = new DistributionLnDaoImpl();
		DistributionLnDto _disLnDto = new DistributionLnDto();
		_disLnDto.setVehicleNo(_disLnVhclNo);

		DistributionLnDto disLnDto = null;
		List<DistributionLnDto> _disSemenList = _disLnDao.getDistributionLns(_disLnDto);
		if(_disSemenList != null && !_disSemenList.isEmpty()){
			disLnDto = _disSemenList.get(0);
		}

		return disLnDto;
	}//end getDisLNOnVehcleNo

	/**
	 * Method called to get the ProcurementLN based on the passed FromStaton
	 * @param _disLnFrmStat
	 * @return
	 * @throws Exception
	 */

	public DistributionLnDto getDisLNOnFrmStat(final String _disLnFrmStat) throws Exception {
		final DistributionLnDao _disLnDao = new DistributionLnDaoImpl();
		DistributionLnDto _disLnDto = new DistributionLnDto();
		_disLnDto.setFrmStation(_disLnFrmStat);

		DistributionLnDto disLnDto = null;
		List<DistributionLnDto> _disSemenList = _disLnDao.getDistributionLns(_disLnDto);
		if(_disSemenList != null && !_disSemenList.isEmpty()){
			disLnDto = _disSemenList.get(0);
		}

		return disLnDto;
	}//end getDisLNOnFrmStat

	/**
	 * Method called to get the ProcurementLN based on the passed PrLN Object
	 * @param _disLnDto
	 * @return
	 * @throws Exception
	 */

	public DistributionLnDto getDisLN(final DistributionLnDto _disLnDto) throws Exception {
		final DistributionLnDao _disLnDao = new DistributionLnDaoImpl();

		DistributionLnDto disLnDto = null;
		List<DistributionLnDto> _disSemenList = _disLnDao.getDistributionLns(_disLnDto);
		if(_disSemenList != null && !_disSemenList.isEmpty()){
			disLnDto = _disSemenList.get(0);
		}

		return disLnDto;
	}//end getDisLN

	/**
	 * Method called to get the list of ProcurementLN based on the passed PrLN Object
	 * @param _disLnDto
	 * @return
	 * @throws Exception
	 */

	public List<DistributionLnDto> getDisLNs(final DistributionLnDto _disLnDto) throws Exception {
		final DistributionLnDao _disLnDao = new DistributionLnDaoImpl();
		return _disLnDao.getDistributionLns(_disLnDto);
	}//end getDisLNs

	/**
	 * Method called to search for the ProcurementLNs based on the passed PrLN Object
	 * @param _disLnDto
	 * @return
	 * @throws Exception
	 */

	public List<DistributionLnDto> searchDisLNs(DistributionLnDto _disLnDto) throws Exception {
		try{
			final DistributionLnDao _disLnDao = new DistributionLnDaoImpl();
			return _disLnDao.searchDistributionLns(_disLnDto);
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.DATA_VIEW_MANTAN_DISTRBUTIONLN_EXCEPTION));
		}
	}//end searchDisLNs
	
	/**
	 * Method called to get the AI based on the passed Code
	 * @param _aiCode
	 * @return
	 * @throws Exception
	 */
	@Override
	public DistributionLnDto getDisLnOnCode(final String _code) throws Exception {
		final DistributionLnDao _disLnDao = new DistributionLnDaoImpl();
		DistributionLnDto _disLnDto = new DistributionLnDto();
		_disLnDto.setCode(_code);

		DistributionLnDto disLnDto = null;
		List<DistributionLnDto> _disLnList = _disLnDao.getDistributionLns(_disLnDto);
		if(_disLnList != null && !_disLnList.isEmpty()){
			disLnDto = _disLnList.get(0);
		}

		return disLnDto;
	}//end getAIOnCode



}// end DistributionLnCtlrImpl
